package com.android.advertising;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ArticleAdapter extends BaseAdapter {
	private List<Article> listArticle;
	private Context context;
	public ImageLoader imageLoader;

	public ArticleAdapter(Context context, List<Article> listArticle) {
		this.listArticle = listArticle;
		this.context = context;
		imageLoader = new ImageLoader(context);
	}

	public List<Article> getListArticle() {
		return listArticle;
	}

	public void setListArticle(List<Article> listArticle) {
		this.listArticle = listArticle;
	}

	@Override
	public int getCount() {
		return listArticle.size();
	}

	@Override
	public Object getItem(int arg0) {
		return listArticle.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	/********* Create a holder Class to contain inflated xml file elements *********/
	public static class ViewHolder {

		public TextView title;
		public TextView time;
		public ImageView image;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder;
		Article article = (Article) getItem(position);
		if (v == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			v = infalInflater.inflate(R.layout.article_recent_list_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) v.findViewById(R.id.title);
			holder.time = (TextView) v.findViewById(R.id.time);
			holder.image = (ImageView) v.findViewById(R.id.imageView1);
			/************ Set holder with LayoutInflater ************/
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		View layout = v.findViewById(R.id.layout_item);
		if (listArticle.get(position).getIsRead() == 1) {
			layout.setBackgroundResource(R.color.list_read);
		} else {
			layout.setBackgroundResource(R.color.list_unread);
		}
		// imageLoader.DisplayImage(article.getThumbLink(), holder.image);
//		String thumbLink = "http://anh.24h.com.vn/upload/4-2012/images/2012-11-06/1352187265_mu-2.jpg";
		String thumbLink = article.getThumbLink();
		imageLoader.DisplayImage(thumbLink, holder.image);
		holder.title.setText(article.getTitle());
		holder.time.setText(Utils.formatTime(article.getCreatedTime()));
		return v;
	}

}
